<?php
	define('index', 1);
	include_once 'login.php';
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Ejemplo de PHP y MySQL</title>
        <link href="css/estilo.css" type="text/css" rel="stylesheet">
    </head>
    <body>
		<?php
			include_once 'db.php';
			include_once 'orden.php';
			openDB();
			selectDB("iaw_db");

			printUserData();
		?>
        <div class="titulo">
            <h1>Contactos</h1>
        </div>
        <div class="titulo">
            <h2>Visitas: 
			<?php
				$visitas = 0;
				if (isset($_SESSION['visitas']))
					$visitas = $_SESSION['visitas'];
            	$visitas = $visitas + 1; 
            	$_SESSION['visitas'] = $visitas;
            	echo $visitas;
			?>
            </h2>
        </div>
        <div class="contenido">
            <table>
                <thead>
                    <tr>
                    	<?php 
//                          <th><a href="./?col=apellido&ord=asc">Apellido</a></th>
                    		$columnas = array("mail", "apellido", "nombre", "direccion", "telefono");

                    		foreach ($columnas as $columna) {
	                    		$nextOrder = getOrder($columna);
	                    		echo "<th>";
		                        echo "<a href='./?col=$columna&ord=$nextOrder'>$columna";
	                            	if (isOrderedBy($columna)) {
	                            		$currentOrder = inverseOrder($nextOrder);
	                            		echo "<img style='border:none;' alt='Orden' src='img/orden_$currentOrder.png'>";
	                            	} 
		                        echo "</a>";
		                        echo "</th>";
                    		}
                    		if (isLoggedIn())
                    			echo "<th></th>";
                    	?>
                    </tr>
                </thead>
                <tbody>
					<?php
					//obtengo los contactos
					$contactos = queryDB("SELECT * FROM contactos ORDER BY $columnaOrdenada $orden");
					$estilo = 0;
					//mientras haya contactos, los agrego a la tabla
					while ($contacto = getRow($contactos)) {
						echo " <tr class=\"alt$estilo\"/>";
						echo "  <td>$contacto[0]</td>";
						echo "  <td>$contacto[1]</td>";
						echo "  <td>$contacto[2]</td>";
						echo "  <td>$contacto[3]</td>";
						echo "  <td>$contacto[4]</td>";
                    	if (isLoggedIn())
							echo "  <td class='opciones'><form action='delete.php' method='post'>
											<input type='hidden' value='$contacto[0]' name='mail' />
											<input type='image' width='16' height='16' src='img/eliminar.png' alt='Eliminar' title='Eliminar'/>
										</form>
									</td>";
						echo " </tr>";
						$estilo = ($estilo + 1) % 2;
					}
					if (isLoggedIn()) {
					?>
					<form action='add.php' method='post'>
						<tr class="agregar">
							<td>
								<input type='text' name='mail'/>
							</td>
							<td>
								<input type='text' name='apellido'/>
							</td>
							<td>
								<input type='text' name='nombre'/>
							</td>
							<td>
								<input type='text' name='direccion'/>
							</td>
							<td>
								<input type='text' name='telefono'/>
							</td>
							<td>
								<input type='image' src='img/agregar.png' alt='Agregar' title='Agregar'/>
							</td>
						</tr>
					</form>
					<?php }?>
                </tbody>
            </table>
        </div>
    </body>
</html>

